<?php

class IndexController extends Zend_Controller_Action
{
	public function indexAction()
	{
	    echo <<<EOD
<html>
<head>
    <style type="text/css">
    body {
        font-family: tahoma, arial, helvetica;
        font-size: 1em;
    }
    h1 {
        font-size: 1.2em;
    }
    h2 {
        font-size: 1.1em;
    }
    #results {
        border: 1px solid darkgrey;
        background-color: #E0E0E0;
    }
    hit {
        border: 1px solid red;
        margin: 2px;
        display: block;
    }
    url,lastChanged,score {
        display: block;
        padding: 2px;
    }
    </style>
    <script src="http://yui.yahooapis.com/2.3.1/build/yahoo/yahoo-min.js"></script>
    <script src="http://yui.yahooapis.com/2.3.1/build/event/event-min.js"></script>
    <script src="http://yui.yahooapis.com/2.3.1/build/connection/connection-min.js"></script> 
    <script type="text/javascript">
    function responseSuccess(r) {
        alert('Woohoo!');
        document.getElementById('results').innerHTML = r.responseText;
    }
    function responseFailure(r) {
        alert("Doh! " + r.responseText);
        document.getElementById('results').innerHTML = r.responseText;
    }
    function search(phrase, catalog, rows, start) {
        var sUrl = '/rest/select?q=' + phrase + '&catalog=' + catalog;
        if (rows != "") {
            sUrl += "&rows=" + rows;
        }
        if (start != "") {
            sUrl += "&start=" + start;
        }
        var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, { success: responseSuccess,
	                                                                     failure: responseFailure
	                                                                   }, null);    
    }
    </script>
</head>
<body>
    <h1>Marjory index search:</h1>
    <form action="javascript:;" onsubmit="search(this.phrase.value, this.catalog.value, this.rows.value, this.start.value)">
        <label for="phrase">Search for:</label>
        <input type="text" name="phrase" value="" />
        <label for="catalog">In catalog:</label>
        <select id="catalog" name="catalog">
            <option value="default" selected="selected">[Default catalogue]</option>
EOD;
        foreach (new DirectoryIterator(dirname(__FILE__) . '/../../data') as $dir) {
            if ($dir != 'default' && !$dir->isDot() && $dir != '.svn') {
                echo "<option value=\"$dir\">$dir</option>";
            }
        }
        echo <<<EOD
        </select><br />
        <label for="rows">Number of rows:</label><input id="rows" type="text" name="rows" value="" maxlength="3" size="3" />
        <label for="start">Start at result #:</label><input id="start" type="text" name="start" value="" maxlength="3" size="3" />
        <br />
        <input type="submit" value="Submit" name="submit" /><br />
    </form>
    <h2>Results:</h2>
    <div id="results"></div>
</body>
</html>
EOD;
	}
	
	public function addAction() {
        echo <<<EOD
<html>
<head>
    <style type="text/css">
    body {
        font-family: tahoma, arial, helvetica;
        font-size: 1em;
    }
    h1 {
        font-size: 1.2em;
    }
    h2 {
        font-size: 1.1em;
    }
    #results {
        border: 1px solid darkgrey;
        background-color: #E0E0E0;
    }
    </style>
    <script src="http://yui.yahooapis.com/2.3.1/build/yahoo/yahoo-min.js"></script>
    <script src="http://yui.yahooapis.com/2.3.1/build/event/event-min.js"></script>
    <script src="http://yui.yahooapis.com/2.3.1/build/connection/connection-min.js"></script> 
    <script type="text/javascript">
    function responseSuccess(r) {
        alert('Woohoo!');
        document.getElementById('results').innerHTML = r.responseText;
    }
    function responseFailure(r) {
        alert("Doh! " + r.responseText);
        document.getElementById('results').innerHTML = r.responseText;
    }
    function add(url, catalog, content) {
        var sUrl = '/rest/update/'; 
        var postData = '<add catalog="' + catalog + '"><doc src="' + url + '">';
        if (content == "") {
            content = "0";
        }
        //postData += '<field name="content">' + content + "</field>";
        //postData += '<field name="abstract">Abstract for ' + url + '</field></doc></add>';
        postData += '</doc></add>';
        var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, { success: responseSuccess,
                                                                         failure: responseFailure
                                                                        }, postData);    
    }
    function addCatalog(title) {
    	var sUrl = '/rest/catalog/';
    	var postData = '<add catalog="' + title + '" />';
    	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, { success: responseSuccess,
    																	  failure: responseFailure
    																    }, postData);
    }
    </script>
</head>
<body>
    <h1>Marjory index addition:</h1>
    <form action="javascript:;" onsubmit="add(this.url.value, this.catalog.value, this.content.value)">
        <label for="url">URL to add:</label>
        <input type="text" id="url" name="url" value="" />
        <label for="catalog">Catalogue to add to:</label>
        <select id="catalog" name="catalog">
        	<option value="default" selected="selected">[Default catalogue]</option>
EOD;
        foreach (new DirectoryIterator(dirname(__FILE__) . '/../../data') as $dir) {
            if ($dir != 'default' && !$dir->isDot() && $dir != '.svn') {
                echo "<option value=\"$dir\">$dir</option>";
            }
        }
        echo <<<EOD
        </select>
        <br />
        <label for="content">Content (leave empty when entering a URL):</label><br />
        <textarea name="content" id="content"></textarea>
        <input type="submit" value="Submit" name="submit" />
    </form>
    <br /><br />
    <form action="javascript:;" onsubmit="addCatalog(this.title.value)">
    	<label for="title">Add new catalog:</label>
    	<input type="text" id="title" name="title" value="" />
    	<input type="submit" value="Submit" name="submit" />
    </form>
    <h2>Results:</h2>
    <div id="results"></div>
</body>
</html>

EOD;
	}
}
